<template>
  <div>
    <div>
      <van-sticky>
        <el-card shadow="nover" class="mobile-card-search">
          <el-row :gutter="20">
            <el-col :span="20">
              <el-input
                placeholder="请输入申请人名称"
                prefix-icon="el-icon-search"
                v-model="queryParams.userName"
                @keyup.enter.native="handleQuery"
              ></el-input>
            </el-col>
            <el-col :span="4">
              <i
                :style="{ color: colorFlag ? '#1890ff' : '' }"
                class="el-icon-s-operation search-icon"
                @click="showPicker = true"
              ></i>

              <!-- 搜索弹窗 -->
              <van-popup
                v-model="showPicker"
                position="bottom"
                round
                style="height: 80%"
                close-icon="close"
                closeable
                @close="popupClose"
                class="modal-content"
              >
                <h3 style="text-align: center">筛选</h3>
                <van-form class="modal-body" ref="queryForm">
                  <searchable-picker
                    v-model="queryParams.isDispose"
                    :columns="dict.type.is_dispose"
                    valueKey="value"
                    labelKey="label"
                    label="是否处理"
                    placeholder="点击选择是否处理"
                  />
                </van-form>
                <div style="" class="modal-footer">
                  <van-button
                    type="default"
                    style="width: 40%; bcost-radius: 10px"
                    @click="resetQuery"
                    >重置</van-button
                  >
                  <van-button
                    type="info"
                    style="width: 40%; bcost-radius: 10px"
                    @click="handleQuery"
                    >查询</van-button
                  >
                </div>
              </van-popup>
              <!-- 搜索弹窗 -->
            </el-col>
          </el-row>
        </el-card>
      </van-sticky>
    </div>
    <div class="mobile-container">
      <van-list
        v-model="loading"
        :finished="finished"
        @load="getList"
        finished-text="没有更多了...."
      >
        <div v-if="costList.length > 0">
          <el-card
            class="mobile-card"
            shadow="hover"
            v-for="cost in costList"
            :key="cost.costId"
          >
            <div slot="header">
              <span class="mobile-card-titel"
                >{{ cost.userName }} 的差旅费明细</span>
              <van-popover
                placement="bottom-end"
                style="float: right"
                v-model="cost.flag"
                trigger="click"
                :actions="actions"
                @select="onSelect($event, cost)"
                @closed="
                  () => {
                    cost.flag = false;
                  }
                "
              >
                <template #reference>
                  <i
                    class="el-icon-more"
                    @click.stop="cost.flag = true"
                  ></i>
                </template>
              </van-popover>
            </div>
            <div style="color: gray; font-size: small">申请人所在部门: </div>
            <div style="margin: 10px 0; font-size: medium">
              <div v-if="cost.deptName"> {{ cost.deptName }}</div>
              <div v-else> -- </div>
            </div>
            <div style="color: gray; font-size: small">是否处理</div>
            <div style="margin: 10px 0; font-size: medium">
              <dict-tag :options="dict.type.is_dispose" :value="cost.isDispose" v-if="cost.isDispose"/>
              <div v-else> -- </div>
            </div>
            <div style="color: gray; font-size: small">出差事由：</div>
            <div style="margin: 10px 0; font-size: medium">
              <div v-if="cost.businessTrip"> {{ cost.businessTrip }}</div>
              <div v-else> -- </div>
            </div>
            <div style="color: gray; font-size: small">费用归属：</div>
            <div style="margin: 10px 0; font-size: medium">
              <dict-tag :options="dict.type.sys_data_ascription" :value="cost.applyAscription" v-if="cost.applyAscription"/>
              <div v-else> -- </div>
            </div>
            <div style="color: gray; font-size: small">归属小组：</div>
            <div style="margin: 10px 0; font-size: medium">
              <dict-tag :options="dict.type.crm_group" :value="cost.applyGroupId" v-if="cost.applyGroupId"/>
              <div v-else> -- </div>
            </div>
            <div style="color: gray; font-size: small">所属人：</div>
            <div style="margin: 10px 0; font-size: medium">
              <div v-if="cost.applyUserName"> {{ cost.applyUserName }}</div>
              <div v-else> -- </div>
            </div>
            <div style="color: gray; font-size: small">所属部门：</div>
            <div style="margin: 10px 0; font-size: medium">
              <div v-if="cost.applyDeptName">{{ cost.applyDeptName }}</div>
              <div v-else> -- </div>
            </div>
            <div style="color: gray; font-size: small">创建时间：</div>
            <div style="margin: 10px 0; font-size: medium">
              <div v-if="cost.createTime">{{ parseTime(cost.createTime, "{y}-{m}-{d}") }}</div>
              <div v-else> -- </div>
            </div>
          </el-card>
        </div>
        <van-empty description="暂无数据" v-if="costList.length === 0 && !loading"/>
      </van-list>
    </div>

    <van-popup
      v-model="show"
      round
      position="bottom"
      :style="{ height: '80%' }"
      closeable
      close-icon="close"
    >
      <h3 style="text-align: center">设置费用归属</h3>
      <van-form @submit="submitForm" style="margin-top: 30px">
        <searchable-picker
          v-model="form.applyAscription"
          :columns="dict.type.sys_data_ascription"
          valueKey="value"
          labelKey="label"
          label="费用归属"
          placeholder="请选择费用归属"
          :rules="[{ required: true, message: '请选择费用归属' }]"
        />
        <searchable-picker
          v-model="form.applyGroupId"
          :columns="dict.type.crm_group"
          v-if="form.applyAscription === '2'"
          valueKey="value"
          labelKey="label"
          label="所属小组"
          placeholder="请选择所属小组"
          :rules="[{ required: true, message: '请选择所属小组' }]"
        />
        <DeptTreePicker
            ref="treePicker"
            v-model="form.applyDeptId"
            v-if="form.applyAscription === '1'"
            label="所属部门"
            :rules="[{ required: true, message: '请选择归属部门' }]"
        />
        <searchable-picker
          v-model="form.applyUserId"
          v-if="form.applyAscription === '0'"
          :columns="userList"
          valueKey="userId"
          labelKey="userName"
          label="所属人"
          placeholder="请选择所属人"
          :rules="[{ required: true, message: '请选择所属人' }]"
        />
        <searchable-picker
          v-model="form.customerId"
          :columns="customerList"
          valueKey="customerId"
          labelKey="customerName"
          label="客户"
          placeholder="选择客户"
          :rules="[{ required: true, message: '请选择客户' }]"
          @change="getOrderByCustomerIdAndProgress"
        />
        <searchable-picker
          v-model="form.orderId"
          :columns="addOrderList"
          valueKey="orderId"
          labelKey="orderName"
          label="商机"
          placeholder="选择商机"
          :rules="[{ required: true, message: '请选择商机' }]"
        />
        <div style="margin: 16px">
          <van-button round block type="info" native-type="submit"
            >确定</van-button
          >
        </div>
      </van-form>
    </van-popup>
  </div>
</template>
<script>

import {
  listCost,
  getCost,
  updateCost,
} from "@/api/crm/cost";
export default {
  name: "costListMobile",
  dicts: ["is_dispose", "sys_data_ascription", "crm_group"],
  props: {
    userList: {
      type: Array,
      default: [],
    },
    customerList: {
      type: Array,
      default: [],
    },
    orderList: {
      type: Array,
      default: [],
    }
  },
  data() {
    return {
      actions: [{ text: "处理费用", icon: "balance-o" }],
      // 部门名称
      deptName: undefined,
      showPicker: false,
      // 遮罩层
      loading: false,
      finished: false,
      costList: [],
      // 总条数
      total: 0,
      // 查询参数
      queryParams: {
        pageNum: 1,
        pageSize: 10,
      },
      colorFlag: false,
      flag: false,
      show: false,
      form: {},
      // orderList: [],
      addOrderList: []
    };
  },
  mounted () {
  },
  methods: {
    getOrderByCustomerIdAndProgress() {
      this.addOrderList = this.orderList.filter(item => item.customerId === this.form.customerId);
    },
    /** 提交按钮 */
    submitForm() {   
        updateCost(this.form).then((response) => {
            this.$toast.success("修改成功");
            this.show = false
            this.handleQuery()
        });
    },
    onSelect(action, cost) {
      this.handleUpdate(cost)
    },
    /** 修改按钮操作 */
    handleUpdate(row) {
      getCost(row.id).then((response) => {
        this.form = response.data;
        this.show = true
        this.addOrderList = this.orderList.filter(item => item.customerId === this.form.customerId);
      });
    },
    popupClose() {
      if (
        this.queryParams.isDispose
      ) {
        this.colorFlag = true;
      } else {
        this.colorFlag = false;
      }
    },
    /** 查询项目列表 */
    async getList() {
      this.loading = true;
      try {
        let obj = JSON.parse(JSON.stringify(this.queryParams));
        let res = await listCost(obj);
        // 判断是否加载完所有数据
        if (this.costList.length >= res.total) {
          this.finished = true;
        } else {
          
          this.costList.push(
            ...res.rows.map((item) => ({
              ...item,
              flag: false,
            }))
          );

          this.queryParams.pageNum += 1;
          this.$forceUpdate()
        }
      } catch (error) {
        console.error("加载数据失败", error);
      } finally {
        this.loading = false;
      }
    },
    resetQuery() {
      this.queryParams = {
        pageNum: 1,
      };
      this.handleQuery();
      this.colorFlag = false;
    },
    /** 搜索按钮操作 */
    handleQuery() {
      this.costList = [];
      this.queryParams.pageNum = 1;
      this.showPicker = false;
      this.getList();
    },
  },
};
</script>

<style scoped>
.modal-content {
  display: table;
  width: 100%;
  height: 100%;
}

.modal-body {
  display: table-row;
  height: 100%;
}

.modal-footer {
  display: table-row;
  text-align: center;
  padding: 16px;
  background-color: white;
  display: flex;
  justify-content: space-around;
  bcost-top: 1px solid #e4dfdf;
}

.modal-footer button {
  width: 45%;
  bcost-radius: 10px;
}

.submitButton {
  position: fixed;
  top: 85%;
  left: 85%;
}
</style>